/*
 * Application Name: 工作票信息管理系统
 * Desc: 系统主框架
 * Author: Albert 
 * Date: 2009-07-14
 */
Ext.namespace('workpaper');

/******************各子模块必须继承的基类*****************************************************************************************/
//子类必须覆盖基类的init 和 destroy 方法，其中destroy方法用于destroy子类的变量、函数，防止内存泄漏(注意检查所有的组件都人为指定id，便于使用Ext.getCmp()方法获取组件并销毁)

workpaper.baseModule = function(tabPanel,tab){
    this.tabPanel = tabPanel;
    this.tab = tab;
    //子模块公用的代码选择项数据
    this.paperStatusData = [['ytx','已填写'],['yqf','已签发'],['yqs','已签收'],['acyzx','安措已执行'],['yxk','已许可'],['sgks','施工开始'],['yzj','已终结'],['all','全部']];
    this.paperTypeData = [['sbxq','设备消缺单'],['rljx','热力机械工作票'],['rk','热控工作票'],['dqdy','电气第一种工作票'],['dqde','电气第二种工作票'],['ecgzaq','二次工作安全措施票'],['yjdh','一级动火工作票'],['ejdh','二级动火工作票'],['all','全部']];
    //End of 子模块公用的代码选择项数据    
    this.init();    
}   
  
Ext.extend(workpaper.baseModule, Ext.util.Observable, {   
    init : Ext.emptyFn,
    destroy : Ext.emptyFn	   
});
/*****************************************************************************************************************************/




/****************主程序类*******************************************************************************************************/
workpaper.app = function(){  
    this.tree = new Ext.tree.TreePanel({
    	layout:'fit',
        id:'menu-tree',
        region:'west',
        title:'功能菜单',
        split:true,
		//animate:true,//加上这个属性后，鼠标经过tree的滚动条时会有js异常
        width: 150,
        minSize: 60,
        maxSize: 400,
        collapsible: true,
        margins:'0 0 5 5',
        loader: new Ext.tree.TreeLoader({
	        url: '../../serverHTML/desktop/sysmenu.html',
	        requestMethod: 'POST'
	    }),		    
		root: new Ext.tree.AsyncTreeNode({
		    id: 'root_menu',
            text: 'menus',
            expanded:true
		}),        
        rootVisible:false,
        lines:false,
        autoScroll:true,
		tbar: [{
				id:'expandTree',
				iconCls:'tree-expand',
				text:'展开',
				handler: function(){
					Ext.getCmp("menu-tree").expandAll();
					//Ext.getCmp("collapseTree").enable();
					//Ext.getCmp("expandTree").disable();
				},
				scope: this
		},{
				id:'collapseTree',
				iconCls:'tree-collapse',
				text:'收缩',
				handler: function(){
					Ext.getCmp("menu-tree").collapseAll();
					//Ext.getCmp("expandTree").enable();
					//Ext.getCmp("collapseTree").disable();
				},
				scope: this
		}]
    }); 			  

	this.tabBody =  new Ext.TabPanel({            	
				        id:'main-tabs',				        
				        activeTab:0,
				        region:'center',
				        margins:'0 5 5 0',
				        split:true,
				        resizeTabs:true,
				        tabWidth:115,
				        minTabWidth: 90,
				        enableTabScroll:true,				        
				        plugins: new Ext.ux.TabCloseMenu()
				      });				          
    this.waitingNode = new Ext.tree.TreeNode({id:"m101",text:"待办工作票",cls:"forum",iconCls:"icon-forum",jsURL:"../../extModules/dealPaper/waitingPaper.js",leaf:true});	
	this.trackStatTabLoaded = false;//实时跟踪统计tab的数据已加载标志
    this.init();   
}
   
Ext.extend(workpaper.app, Ext.util.Observable, {   
    init: function(){
    	//this.loadMask = new Ext.LoadMask(this.tabBody.body,{msg:'正在加载数据，请稍候。。。',msgCls:'loading-indicator'});
		this.tree.on('click',this.clickTree, this);        
        this.tabBody.on('tabchange', this.changeTab, this); 
        this.tabBody.on('beforeremove', this.tabBeforeRemove, this);  
        var viewport = new Ext.Viewport({   
            layout:'border',   
            items:[   
	            new Ext.BoxComponent({
	            	layout:'fit',
	                region:'north',
	                el: 'appHeader',
					margins:'0 5 0 5',
	                height:75
	            }),                
                this.tree,  
                this.tabBody   
            ]   
        });        
    },
    
    //切换TabPanel标签方法
    changeTab: function(tabPanel,tab){
        //如果存在相应树节点，就选中,否则就清空选择状态
        var id = tab.id.replace('tab-','');
        var node = this.tree.getNodeById(id);
        if(node){
            this.tree.getSelectionModel().select(node);            
            //如果是“实时跟踪统计”tab卡，由于不能保证它已经加载，故在对应的菜单节点上做click动作，保证加载它
            if(node.id == "m201" && !this.trackStatTabLoaded){
	            this.tabBody.setActiveTab(tab);	            
	            //this.loadModel(node,tab,tabPanel);//（项目完成后取消本注销，让本语句有效）
	            this.trackStatTabLoaded = true;          	
            }
        }else{
            this.tree.getSelectionModel().clearSelections();
        }      
    },
    
    //点击菜单方法(node:被点击的节点)
    clickTree: function(node){
        //如果节点不是叶子则返回
        if(!node.isLeaf()){   
            return false;   
        }
        
        var id = 'tab-' + node.id;
        var tab = Ext.getCmp(id);
        if(!tab){ 
            tab = this.tabBody.add(new Ext.Panel({
                id: id,                
                title: node.text,
                layout: 'fit',
                border:false,
                margins:'0 0 0 0',
                closable: true
            }));        	

            this.tabBody.setActiveTab(tab);  
            if(node.attributes.jsURL != ""){//临时使用本判断
            	this.loadModel(node,tab,this.tabBody);//加载模块
            }          
            
        }else{
            this.tabBody.setActiveTab(tab);
        }
    },
    
    //加载模块方法(node:菜单节点; tab:模块显示在哪里; tabPanel:组件tabPanel)
    loadModel: function(node,tab,tabPanel){        
        var id = node.id;
        var tabId = "tab-" + id;
        
        //用this[id]表示模块子类的类名称（同时也定义了这个类，等于动态装载了对应的js文件）
        //用this[tabId]表示模块变量，便于关闭tab时销毁       
        if(this[id]){
            //如果模块类已存在，就直接实例化
            this[tabId] = new this[id](tabPanel,tab);
        }else{
        	if(!this.loadMask){
        		this.loadMask = new Ext.LoadMask(this.tabBody.body,{msg:'正在加载数据，请稍候。。。',msgCls:'loading-indicator'});
        	}
            this.loadMask.show();   
            Ext.Ajax.request({   
                method:'POST',
                url: node.attributes.jsURL,   
                scope: this,   
                success: function(response){
                    //获取模块类  
                    this[id] = eval(response.responseText);   
                    //实例化模块类
                    this[tabId] = new this[id](tabPanel,tab);                    
                    this.loadMask.hide();   
                }   
            });
        }
    },
    
    //自动加载“待办工作票”
    autoLoadWaitingTab: function(){
        var id = 'tab-' + this.waitingNode.id;        
        tab = this.tabBody.add(new Ext.Panel({
            id: id,            
            title: this.waitingNode.text,
            layout: 'fit',
            border:false,
            margins:'0 0 0 0',
            closable: false
        }));        
        //加载模块
        this.loadModel(this.waitingNode,tab,this.tabBody); 
        this.tabBody.setActiveTab(tab);       
    },    
    
    //销毁：防止内存泄漏
    tabBeforeRemove : function(tabContainer,tab){ 
    	if(this[tab.id]) this[tab.id].destroy();//调用子模块对象的destroy方法销毁其内部变量     	
    	Ext.destroy(this[tab.id]);    	
    	this[tab.id] = null;//销毁子模块对象本身
    	Ext.destroy(tab);
    	
    	var id = tab.id.replace('tab-','');
    	Ext.destroy(this[id]);//销毁子模块的类定义（项目完成后注销本语句） 
    	this[id] = null;//（项目完成后注销本语句）
    }   
});   

/*****************************************************************************************************************************/

/****************实例化主程序类**************************************************************************************************/
  Ext.BLANK_IMAGE_URL = '../../extSDK/resources/images/default/s.gif';//注意路径
  Ext.QuickTips.init();
  Ext.form.Field.prototype.msgTarget = 'side';
  Ext.state.Manager.setProvider(new Ext.state.CookieProvider()); //初始化Ext状态管理器，在Cookie中记录用户的操作状态，如果不启用，象刷新时就不会保存当前的状态，而是重新加载 
  
Ext.onReady(function(){ 
    myApp = new workpaper.app();        
    //myApp.autoLoadWaitingTab();//待办工作票
    
    //添加“实时跟踪统计”tab ，但不加载数据
    /*
    var id = 'tab-m201';
    var trackstatTab = myApp.tabBody.add(new Ext.Panel({
        id: id,
        title: '实时跟踪统计',
        layout: 'fit',
        closable: false
    })); 
     */   
          
	new Ext.ux.ThemeCycleButton({renderTo : Ext.get("styleSpan")});//渲染按钮，用于风格样式切换
	//new Ext.ux.CheckMemoryCycleButton({renderTo : Ext.get("checkMemorySpan")});	//查看内存泄漏时重复出现的组件，用于检查内存泄漏
	Ext.get('loading').remove();
	Ext.get('loading-mask').fadeOut({remove:true});
	//window.resizeTo(1024,768);
	
});

/*****************************************************************************************************************************/
